/**
 * Copyright ©2016-2020 northkingbpo Corporation, All Rights Reserved
 * http://www.northkingbpo.net/
 */
package profile.hujia.simulation.data.service;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import profile.hujia.simulation.data.config.SystemConfig;
import profile.hujia.simulation.data.domain.SingleBusinessLabelArg;
import profile.hujia.simulation.data.domain.SingleLabelArg;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * class info
 *
 * @author hujia
 * @date 2019-06-14 11:02
 */
@Data
@Slf4j
public class BusinessQueue {

    private HashMap<Integer, List<Integer>> queueMap;
    private HashMap<Integer, HashMap<String, List<Integer>>> specificQueueMap;
    private HashMap<String, List<Integer>> labelQueueMap;
    private SingleLabelArg singleLabelArg;
    private SingleBusinessLabelArg singleBusinessLabelArg;

    public BusinessQueue(SingleLabelArg singleLabelArg, SingleBusinessLabelArg singleBusinessLabelArg) {
        this.singleLabelArg = singleLabelArg;
        this.singleBusinessLabelArg = singleBusinessLabelArg;
        queueMap = new HashMap<>();
        for (int i = 0; i < SystemConfig.TOTAL_BUSINESS_TYPE; ++i) {
            List<Integer> queuePerson = new ArrayList<>();
            queueMap.put(i + 1, queuePerson);
        }
        labelQueueMap = new HashMap<>();
        for (int i = 0; i < SystemConfig.SINGLE_NAME.length; ++i) {
            List<String> values = singleLabelArg.getLevelMap().get(SystemConfig.SINGLE_NAME[i]);
            for (int j = 0; j < values.size(); ++j) {
                List<Integer> queue = new ArrayList<>();
                labelQueueMap.put(SystemConfig.SINGLE_NAME[i] + "-" + values.get(j), queue);
            }
        }
        specificQueueMap = new HashMap<>();
        for (int i = 0; i < SystemConfig.TOTAL_BUSINESS_TYPE; ++i) {
            specificQueueMap.put(i + 1, new HashMap<>());
            for (int j = 0; j < SystemConfig.SINGLE_BUSINESS_NAME.length; ++j) {
                List<String> values = singleBusinessLabelArg.getLevelMap().get(SystemConfig.SINGLE_BUSINESS_NAME[j]);
                for (int k = 0; k < values.size(); ++k) {
                    List<Integer> queue = new ArrayList<>();
                    specificQueueMap.get(i + 1).put(SystemConfig.SINGLE_BUSINESS_NAME[j] + "-" + values.get(k), queue);
                }
            }
        }
    }

    public void statisticBusiness(Integer businessName, Integer personIndex) {
        List<Integer> queuePerson = queueMap.get(businessName);
        queuePerson.add(personIndex);
    }

    public Integer getQueueLength(Integer businessName) {
        return queueMap.get(businessName).size();
    }

    public List<Integer> getQueue(Integer businessName) {
        return queueMap.get(businessName);
    }

    public void statisticLabel(String label, String labelLevel, Integer personIndex) {
        String key = label + "-" + labelLevel;
        labelQueueMap.get(key).add(personIndex);
    }

    public void statisticBusinessLabel(Integer businessIndex, String label, String labelLevel, Integer personIndex) {
        String key = label + "-" + labelLevel;
        specificQueueMap.get(businessIndex).get(key).add(personIndex);
    }

    public void intersection(List<String> condition) {
        condition.forEach(str -> {
            String[] arrays = str.split(SystemConfig.SEPARATION_CHARACTER);
            List<Integer> result = null;
            for (int i = 0; i < arrays.length; ++i) {
                String[] keys = arrays[i].split("~");
                List<Integer> tmp;
                if (keys.length == 1) {
                    tmp = labelQueueMap.get(keys[0]);
                } else {
                    tmp = specificQueueMap.get(Integer.parseInt(keys[0])).get(keys[1]);
                }
                if (result == null) {
                    result = new ArrayList<>(tmp);
                } else {
                    result.retainAll(tmp);
                }
            }
            log.info(str + " size is " + result.size());
        });
    }

}
